var BaseDao = require('../../base-dao/BaseDao');
var uuid = require('node-uuid');
var async = require('async');

// 获取签证
exports.getLast = function(cb) {
	var sql = 'SELECT * FROM tbl_signature ORDER BY timestamp DESC LIMIT 0,1';
	BaseDao.execQuery(sql, function(err, docs) {
		cb(err, docs);
	})
}

// new 
exports.getSignature = function(spotid,cb) {
	var sql = 'SELECT cs.* FROM tbl_cp_signature cs,tbl_spot_wx_config swc where swc.spotid="AT009" and cs.appid=swc.waid ORDER BY cs.createAt DESC LIMIT 0,1'
	BaseDao.execQuery(sql, function(err, docs) {
		cb(err, docs.length>0?docs[0]:null);
	})
}

exports.findUserUnoidByUserid = function(userid,cb) {
	var sql = 'SELECT * FROM tbl_user where status=1 and id="'+userid+'"';
	BaseDao.execQuery(sql, function(err, docs) {
		cb(err, docs.length>0?docs[0]:-1);
	})
}

/**
 * 根据unionid，及appid去查询相关用户
 * @param unionid
 * @param spotid
 * @param cb
 */
exports.findWeiXinUserOpenid = function(unionid,cb) {
	//console.log(unionid)//and wu.appid="'+unionid.mediatype+'"
	//var sql = 'SELECT DISTINCT wu.openid,wu.appid FROM tbl_weixin_user wu,tbl_spot_wx_config swc where  swc.waid=wu.appid  and wu.status=1 and wu.unionid="'+unionid.mediauid+'" order by wu.createAt desc';
	var sql='SELECT DISTINCT wu.openid, wu.appid FROM tbl_weixin_user wu WHERE wu. STATUS = 1 AND wu.unionid = "'+unionid.mediauid+'" ORDER BY wu.createAt DESC';
	//console.log(sql)
	BaseDao.execQuery(sql, function(err, docs) {
		cb(err, docs.length>0?docs:-1);
	})
}


exports.findEyouOpenid = function(unionid,appid,cb) {
	//console.log(unionid)//and wu.appid="'+unionid.mediatype+'"
	//var sql = 'SELECT DISTINCT wu.openid,wu.appid FROM tbl_weixin_user wu,tbl_spot_wx_config swc where  swc.waid=wu.appid  and wu.status=1 and wu.unionid="'+unionid.mediauid+'" order by wu.createAt desc';
	var sql='SELECT DISTINCT wu.openid, wu.appid FROM tbl_weixin_user wu WHERE wu. STATUS = 1 and wu.appid="'+appid+'" AND wu.unionid = "'+unionid.mediauid+'" ORDER BY wu.createAt DESC';
	//console.log(sql)
	BaseDao.execQuery(sql, function(err, docs) {
		cb(err, docs.length>0?docs:-1);
	})
}


// 查询
exports.select = function(appid,cb) {//wx538040febfc4cd83
	var sql = 'SELECT * FROM tbl_cp_signature where appid="'+appid+'" ORDER BY createAt DESC LIMIT 0,1';
	BaseDao.execQuery(sql, function(err, doc) {
		cb(err, doc);
	})
}

exports.getTicket = function (cb) {
	var sql = 'SELECT * FROM tbl_cp_signature ORDER BY createAt DESC LIMIT 0,1';
	BaseDao.execQuery(sql, function (err, data) {
		var res;
		if(data && data.length>0) {
			res = data[0];
		}
		cb(err, res);
	})
}

exports.findUserByOpenid=function(openid,appid,callback){
	var sql = 'SELECT openid FROM tbl_weixin_user where status=1 and openid="'+openid+'" and appid="'+appid+'"';
	BaseDao.execQuery(sql, function(err, docs) {
		//console.log(docs)
		callback(err, docs.length>0?docs[0].openid:-1);
	})
}

exports.getWxCodeList=function(callback){
	var sql = 'SELECT * from tbl_wx_appid';
	BaseDao.execQuery(sql, function(err, docs) {
		callback(err, docs);
	})
}
/**
 * 根据appid获取token
 * @param appid
 * @param callback
 */
exports.getTokenbyappid=function(appid,callback){
	console.log(appid)
	var sql = 'SELECT * from tbl_cp_signature where appid="'+appid+'" order by createAt desc limit 0,1';
	console.log(sql)
	BaseDao.execQuery(sql, function(err, docs) {
		console.log(err)
		callback(err, docs?docs[0]:{});
	})
}